<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <ul>
        <li
          v-for="item in goodsList"
          :key="item.id"
          @click="goToDetail(item.id)"
        >
          <img
            :src="item.list_pic_url"
            alt=""
            width="100%"
            style="display: block"
          />
          <div class="title">{{ item.name }}</div>
          <div class="price">{{ item.retail_price | filtermoney }}</div>
        </li>
      </ul>
    </van-pull-refresh>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  name: "Product",
  data() {
    return {
      isLoading: false,
    };
  },
  props: ["name"],
  methods: {
    // 跳转到商品详情页
    async goToDetail(id) {
      await this.$router.push("/productdetail/" + id);
    },
    onRefresh() {
      setTimeout(() => {
        this.isLoading = false;
      }, 1000);
    },
  },
  computed: {
    ...mapGetters(["categoryList"]),
    goodsList() {
      for (const key in this.categoryList) {
        if (this.categoryList[key].name == this.name) {
          return this.categoryList[key].goodsList;
        }
      }
      return this.categoryList;
    },
  },
  mounted() {
    this.$store.dispatch("getHomeDB");
  },
};
</script>

<style lang="less" scoped>
ul {
  background-color: #efefef;
  padding: .1rem 2%;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  li {
    width: 49%;
    background-color: #fff;
    text-align: center;
    line-height: .3rem;
    margin-bottom: .1rem;
    .title {
      // 最多显示一行的文字，多余的内容会被省略
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .price {
      color: darkred;
    }
  }
}
</style>